package cn.mysource.org.service.impl;

import cn.mysource.basic.config.BussinessException;
import cn.mysource.basic.util.BaiduAuditUtils;
import cn.mysource.basic.vo.EchartVo;
import cn.mysource.basic.vo.ShopStateAndCountVo;
import cn.mysource.org.domain.Employee;
import cn.mysource.org.domain.Shop;
import cn.mysource.org.domain.ShopAuditLog;
import cn.mysource.org.mapper.EmployeeMapper;
import cn.mysource.org.mapper.ShopAuditLogMapper;
import cn.mysource.org.mapper.ShopMapper;
import cn.mysource.org.service.IShopService;
import cn.mysource.basic.service.impl.BaseServiceImpl;
import cn.mysource.org.vo.AuditShopVo;
import cn.mysource.org.vo.EmployeeVo;
import cn.mysource.org.vo.ShopSettlementVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-11-20
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper auditLogMapper;
    /**
     * 店铺入住
     *
     * @param vo
     */
    @Override
    @Transactional
    public void settlement(ShopSettlementVo vo) {
        // 参数校验
        this.verify(vo);
        // 保存数据
        this.save(vo);
    }

    @Override
    public void auditPass(AuditShopVo vo) {
        //更改店铺状态为 已激活
        Shop shop = new Shop();
        shop.setId(vo.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        //添加审核日记
        ShopAuditLog log = new ShopAuditLog();
        log.setAuditId(1);
        log.setAuditTime(new Date());
        log.setShopId(vo.getShopId());
        log.setNote(vo.getNote());
        auditLogMapper.add(log);
        //发送邮件告诉用户 让自己封装成工具类
        // 1.创建复杂邮件对象

//        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//        // 2.发送复杂邮件的工具类
//        try {
//            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
//            helper.setFrom("978395359@qq.com");
//            helper.setSubject("测试复杂邮件");
//            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
//            helper.setText("<h1>男德经</h1>"+
//                    "<img src='http://dfs.java.itsource.cn/group1/M00/00/05/oYYBAGKDLDuADv2ZAABvwky9b5015.jpeg\"' />",true);
//            // 4.添加附件
//            helper.addAttachment("好东西.jpg",new File("C:\\Users\\colin\\Desktop\\1.jpg"));
//            // 5.设置收件人
//            helper.setTo("978395359@qq.com");
//            // 6.发送邮件
//            javaMailSender.send(mimeMessage);
//        } catch (MessagingException e) {
//            e.printStackTrace();
//        }
    }

    @Override
    public EchartVo echart() {
        List<ShopStateAndCountVo> list = shopMapper.echart();
        return null;
    }

    private void save(ShopSettlementVo vo) {
        // 保存店铺数据
        Shop insertShop = vo.getShop();
        shopMapper.add(insertShop);
        // 保存员工数据
        Employee emp = vo.getEmp();
        emp.setShopId(insertShop.getId());
        employeeMapper.add(emp);
        // 修改店铺关联的店长id
//        insertShop.setAdminId(emp.getId());
        Shop updateShop = new Shop();
        updateShop.setId(insertShop.getId());
        updateShop.setAdminId(emp.getId());
        shopMapper.update(updateShop);
    }

    private void verify(ShopSettlementVo vo) {
        // 校验参数
        if (StringUtils.isBlank(vo.getName())){
            throw new BussinessException("店铺名称不能为空！");
        }

        EmployeeVo admin = vo.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BussinessException("两次密码不一致！");
        }
        //店铺是否存在 根据店铺电话 查询店铺数据
        Shop shop = shopMapper.findByPhone(vo.getTel());
        if (null != shop){
            throw new BussinessException("当前店铺已经存在了，不能入驻！");
        }
        // 管理员是否已经管理了店铺
        // 1.根据管理员姓名 查询管理员信息
        Employee employee = employeeMapper.findByName(admin.getUsername());
        if (null != employee && null != employee.getShopId()){
            // 2.该管理员他在店铺里面 但是需要判断他是不是管理员
            shop = shopMapper.findByAdminId(employee.getId());
            if (null != shop && shop.getAdminId().equals(employee.getId())){
                // 他就是管理员
                throw new BussinessException("你已经管理了店铺了");
            }
        }
        // TODO 百度AI

        if(!BaiduAuditUtils.TextCensor(vo.getName())){
            throw new BussinessException("店铺名称不合法！！");
        }
        if(!BaiduAuditUtils.ImgCensor(vo.getLogo())){
            throw new BussinessException("店铺logo不合法！！");
       }
    }
}
